# load("xxx.RData")



### INDIVIDUAL LEVEL VARIABLES

# A) Personal Characteristics

# Experienced author indicator and number:

exper_auth <- authors[names(authorcounts),"pre_experience"] >= K

N_auth_exp <- sum(exper_auth)

# Researcher skill:

Z <- as.character( authors[names(authorcounts), "skill_code"] )

Z[is.na(Z)] <- "000"

Z <- as.factor(Z)

###

# B) Person-Project Interactions

# Indicator for unfamiliar topic:

temp1 <- topic_code[unlist(paperlist)]
temp2 <- rep(Z, lengths(paperlist) )

temp3 <- temp1 != temp2 & (temp1 != "000")

# Author-specific project cost:

X_C_auth <- X_C[unlist(paperlist)] : as.factor(temp3)

# Drop empty levels:

X_C_auth <- droplevels(X_C_auth)

# Project category of each paper (by each author):

X_P_auth <- X_C_auth : X_I[unlist(paperlist)]
	names(X_P_auth) <- unlist(paperlist)

# Drop empty levels:

X_P_auth <- droplevels(X_P_auth)

# Final number of categories:

nlevels(X_C_auth)
nlevels(X_P_auth)

###

# C) Lists of Project Characteristics

# Project category of each paper (by each author):

temp1 <- as.numeric(X_P_auth)

X_list <- relist(temp1, skeleton=paperlist)
	names(X_list) <- names(paperlist)

# Actual impact of each paper (by each author):

temp2 <- as.numeric(articles[unlist(paperlist),"impact"])

Y_list <- relist(temp2, skeleton=paperlist)
	names(Y_list) <- names(paperlist)

# Sort by actual impact:

temp3 <- lapply(Y_list, order, decreasing=TRUE)

for (a in 1:N_auth_full)  {
	temp <- X_list[[a]]
	X_list[[a]] <- temp[temp3[[a]]]
	temp <- Y_list[[a]]
	Y_list[[a]] <- temp[temp3[[a]]]
}

# Keep the predicted impact and category of top *L* papers:

for (a in which(lengths(paperlist) > L))  {
	temp <- X_list[[a]]
	X_list[[a]] <- temp[1:L]
	temp <- Y_list[[a]]
	Y_list[[a]] <- temp[1:L]
}

# Sort by X_P:

temp3 <- lapply(X_list, order, decreasing=TRUE)

for (a in 1:N_auth_full)  {
	temp <- X_list[[a]]
	X_list[[a]] <- temp[temp3[[a]]]
	temp <- Y_list[[a]]
	Y_list[[a]] <- temp[temp3[[a]]]
}

# Clean up:

rm(a, temp, temp1, temp2, temp3)
